//
// Created by konglinglong on 2020/9/4.
//

#include <stdio.h>
#include <iostream>
#include <math.h>
#include "../lib/doctest.h"
#include "../../include/utils/misc.h"

using namespace std;

TEST_SUITE ("MISC_TS")
{
    TEST_CASE ("MISC_TC")
    {
        uint32_t loop;
        uint64_t timeStamp;
        uint64_t lastTick;

        SUBCASE("MISC_SC_BASE")
        {
            CSP_InitCpuFreqVar();
            CSP_DelayStrictUs(&timeStamp, 100000);
            CSP_DelayStrictUs(&timeStamp, 100000);
            CSP_ArrivalDelayTimeUs(&lastTick, 100000);
            usleep(100000);
            REQUIRE(TRUE == CSP_ArrivalDelayTimeUs(&lastTick, 100000));
            usleep(100000);
            REQUIRE(FALSE == CSP_ArrivalDelayTimeUs(&lastTick, 150000));
        }

        SUBCASE("MISC_SC_LOG2_UINT")
        {
            for (loop = 0; loop < 1000000; loop++)
            {
                uint64_t r = (uint64_t)rand() * 1331364656;
                uint32_t v1 = (uint32_t)CSP_FastLog2Uint(r);
                uint32_t v2 = (uint32_t)log2(r);
                REQUIRE(v1 == v2);
            }
        }

        SUBCASE("MISC_SC_LOG10_UINT")
        {
            for (loop = 0; loop < 1000000; loop++)
            {
                uint64_t r = (uint64_t)rand() * 1331364656;
                uint32_t v1 = (uint32_t)CSP_FastLog10Uint(r);
                uint32_t v2 = (uint32_t)log10(r);
                REQUIRE(v1 == v2);
            }
        }

        SUBCASE("MISC_SC_LOG2_FLOAT")
        {
            for (loop = 0; loop < 1000000; loop++)
            {
                uint64_t r = (uint64_t)rand() * 1331364656;
                REQUIRE(log2(r) == doctest::Approx(CSP_FastLog2Float(r)).epsilon(0.0001));
            }
        }

        SUBCASE("MISC_SC_LOG10_FLOAT")
        {
            for (loop = 0; loop < 1000000; loop++)
            {
                uint64_t r = (uint64_t)rand() * 1331364656;
                REQUIRE(log10(r) == doctest::Approx(CSP_FastLog10Float(r)).epsilon(0.0001));
            }
        }
    }
}


